package graphicView;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.List;
import java.util.Observable;
import java.util.Observer;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import model.AnchorPoint;
import model.ClassEditor;
import model.ClassImpl;
import model.RelationshipEditor;

/**
 * Team EBE
 * 
 * RelationshipGUI.java is the functional user interface to edit a relationship
 * 
 */
public class RelationshipGUI extends JPanel implements MouseListener,
		ActionListener, Observer {

	private static final long serialVersionUID = 1L;
	private JFrame relframe;
	private GraphicController controller;

	Vector<ClassGraphicObject> Vcgo;

	ClassGraphicObject cgo;

	// component declarations

	JTextField classSourceField = new JTextField();
	JTextField classSourceAnchorField = new JTextField();
	JList classList;
	JButton confirmClassSourceChoice = new JButton(
			"Confirm Class Source Choice");
	JButton rejectClassSourceChoice = new JButton("Reject Class Source Choice");

	JTextField classDestField = new JTextField();
	JTextField classDestAnchorField = new JTextField();
	JList destList;
	JButton confirmClassDestChoice = new JButton("Confirm Class Dest Choice");
	JButton rejectClassDestChoice = new JButton("Reject Class Dest Choice");

	JButton updateRelationship = new JButton("Update RelationshipImpl");
	JButton cancelChanges = new JButton("Cancel Changes");
	JButton finishChanges = new JButton("Finish Changes");

	JScrollPane classListScrollPane;
	JScrollPane destListScrollPane;
	JScrollPane relListScrollPane;

	String[] relationshipOptions = { "Aggregation", "Composition", "Uses",
			"Generalization", "Dependancy" };

	JComboBox relOptions = new JComboBox(relationshipOptions);
	JList relList;
	JButton relconfirm = new JButton("Confirm Relationship");
	JButton relreject = new JButton("Reject Relationship");

	DefaultListModel classListModel;
	DefaultListModel destListModel;
	DefaultListModel relListModel;

	int classSourcesAdded;
	int classDestinationsAdded;
	int relationshipsAdded;

	// the relationship that will be edited
	RelationshipEditor theRelEditor;

	// other necessary models
	ClassEditor theClassEditor;
	AnchorPoint theAnchor;

	// vectors of classes used for the sources and destinations
	Vector<ClassImpl> classSource2;
	Vector<ClassImpl> destSource2;

	String classSource;
	String destSource;

	/**
	 * RelationshipGUI utilizes the controller, relframe and theRelEditor to
	 * create the Relationship GUI
	 */
	public RelationshipGUI(GraphicController rController, JFrame rFrame,
			RelationshipEditor Editor) {

		relframe = rFrame;
		controller = rController;
		theRelEditor = Editor;

		classListModel = new DefaultListModel();
		destListModel = new DefaultListModel();
		relListModel = new DefaultListModel();

		initFields();
		JPanel relPanel = setupRelLayout();
		relframe.getContentPane().add(relPanel, BoxLayout.X_AXIS);
	}

	/**
	 * initFields initializes the fields for the relationship panel
	 */
	private void initFields() {
		classSourcesAdded = classDestinationsAdded = relationshipsAdded = 0;

		classListModel.clear();

		classList = new JList(classListModel);

		// create the list and put it in the scroll pane

		classList.setVisibleRowCount(5);

		classListScrollPane = new JScrollPane(classList);

		destListModel.clear();

		destList = new JList(destListModel);

		// create the list and put it in the scroll pane

		destList.setVisibleRowCount(5);

		destListScrollPane = new JScrollPane(destList);

		relListModel.clear();

		// for loop with stuff in it

		relList = new JList(relListModel);

		relList.setVisibleRowCount(5);

		relListScrollPane = new JScrollPane(relList);
	}

	/**
	 * setupRelLayout creates the relationship panel with all of its components
	 * 
	 * @return relPanel returns the relationship panel
	 */
	private JPanel setupRelLayout() {

		// create the main panel for the inspector uses box layout to stack
		// elements vertically
		JPanel relPanel = new JPanel();
		relPanel.setLayout(new BoxLayout(relPanel, BoxLayout.Y_AXIS));

		// sets the command to issue to the listener when the buttons are
		// pressed
		confirmClassSourceChoice.setActionCommand("confirm src");

		confirmClassDestChoice.setActionCommand("confirm dest");

		rejectClassSourceChoice.setActionCommand("reject src");

		rejectClassDestChoice.setActionCommand("reject dest");

		updateRelationship.setActionCommand("relationship");

		cancelChanges.setActionCommand("cancel");

		finishChanges.setActionCommand("finish");

		// creates the action listeners for the buttons

		confirmClassSourceChoice.addActionListener(this);

		confirmClassDestChoice.addActionListener(this);

		rejectClassSourceChoice.addActionListener(this);

		rejectClassDestChoice.addActionListener(this);

		updateRelationship.addActionListener(this);

		cancelChanges.addActionListener(this);

		finishChanges.addActionListener(this);

		// creates and adds the panel that holds the inspectors for the class
		// sources
		JPanel classAndDestSourcePanel = new JPanel();
		classAndDestSourcePanel.setLayout(new BoxLayout(
				classAndDestSourcePanel, BoxLayout.Y_AXIS));

		JPanel classPanel = new JPanel();
		classPanel.setLayout(new GridLayout(1, 2));

		// creating the source editor

		JPanel sourceEditPanel = new JPanel();
		sourceEditPanel.setLayout(new GridLayout(3, 2));
		sourceEditPanel.add(new JLabel("Source"));
		sourceEditPanel.add(classSourceField);
		sourceEditPanel.add(new JLabel("Anchor Point"));
		sourceEditPanel.add(classSourceAnchorField);
		sourceEditPanel.add(confirmClassSourceChoice);
		sourceEditPanel.add(rejectClassSourceChoice);

		classPanel.add(sourceEditPanel);

		// creates the view for the class source selections
		JPanel sourceListPanel = new JPanel();
		sourceListPanel.setLayout(new GridLayout(1, 1));
		sourceListPanel.add(classListScrollPane);
		classPanel.add(sourceListPanel);

		classPanel.setBorder(BorderFactory.createTitledBorder("Class Source"));

		classAndDestSourcePanel.add(classPanel);

		// same as sources except for destinations now
		JPanel destPanel = new JPanel();
		destPanel.setLayout(new GridLayout(1, 2));

		JPanel destEditPanel = new JPanel();
		destEditPanel.setLayout(new GridLayout(3, 2));
		destEditPanel.add(new JLabel("Destination"));
		destEditPanel.add(classDestField);
		destEditPanel.add(new JLabel("Anchor Point"));
		destEditPanel.add(classDestAnchorField);
		destEditPanel.add(confirmClassDestChoice);
		destEditPanel.add(rejectClassDestChoice);

		destPanel.add(destEditPanel);

		// creates the view for the class source selections
		JPanel destListPanel = new JPanel();
		destListPanel.setLayout(new GridLayout(1, 1));
		destListPanel.add(destListScrollPane);
		destPanel.add(destListPanel);

		destPanel.setBorder(BorderFactory
				.createTitledBorder("Class Destination"));

		classAndDestSourcePanel.add(destPanel);

		// create the Relationship type editor
		JPanel relPanel2 = new JPanel();
		relPanel2.setLayout(new GridLayout(1, 2));

		JPanel relEditPanel = new JPanel();
		relEditPanel.setLayout(new GridLayout(2, 2));
		relEditPanel.add(new JLabel("Relationship"));
		relEditPanel.add(relOptions);
		relEditPanel.add(relconfirm);
		relEditPanel.add(relreject);

		relPanel2.add(relEditPanel);

		// creates the view for the relationship selections
		JPanel relListPanel = new JPanel();
		relListPanel.setLayout(new GridLayout(1, 1));
		relListPanel.add(relListScrollPane);
		relPanel2.add(relListPanel);

		relPanel2.setBorder(BorderFactory
				.createTitledBorder("Relationship Type"));

		classAndDestSourcePanel.add(relPanel2);

		relPanel.add(classAndDestSourcePanel);

		// creates a panel for the update and cancel and finish buttons
		JPanel classButtonPanel = new JPanel();
		classButtonPanel.setLayout(new GridLayout(3, 1));
		classButtonPanel.add(updateRelationship);
		classButtonPanel.add(cancelChanges);
		classButtonPanel.add(finishChanges);

		relPanel.add(classButtonPanel);

		relPanel.setPreferredSize(new Dimension(700, 385));

		return relPanel;
	}

	/**
	 * actionPerformed executes when an action occurs
	 */
	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
	}

	/**
	 * mouseClicked is executed when the mouse is clicked on a component
	 */
	@Override
	public void mouseClicked(MouseEvent arg0) {
		// TODO Auto-generated method stub
	}

	/**
	 * mouseEntered is executed when the mouse enters a component
	 */
	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
	}

	/**
	 * mouseExited is executed when the mouse exits a component
	 */
	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
	}

	/**
	 * mousePressed is executed when the mouse is pressed on a component
	 */
	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
	}

	/**
	 * mouseReleased is executed when the mouse is released on a component
	 */
	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
	}

	/**
	 * update simply updates the initialized fields
	 */
	@Override
	public void update(Observable arg0, Object arg1) {
		// TODO Auto-generated method stub
		initFields();
	}

	/**
	 * 
	 */
	public void dimmit() {

		for (ClassGraphicObject classObject : Vcgo) {

			Vcgo.add(classObject);
		}
	}
}